#include <cstdlib>
#include <iostream>
#include <pthread.h>
#include <time.h>
#include "request.h"
#include "monitor.h"
using namespace std;

#define MAX_WORK 100

#define RUNTIME 5

typedef struct threadInput {
   int tid;
   monitor* mon;
}  threadInput;

void *client(void *ptr)
{		
	int disk_track = 0;
	int disk_work = 1;
	//char result;
	request* clientReq = new request(0, 1);
	bool req_passed = false;
	threadInput *data = (threadInput*)ptr;
	
	time_t start_time, cur_time;
	double run_time = RUNTIME;
	
	start_time = time(NULL);
	cur_time = time(NULL);

	while(difftime(cur_time, start_time) < run_time){
		//initialize random seed
		srand (clock());

		//generate random values for the track number and amount of work to be done.
		//disk_track is in the range [0, 15], and disk_work is in the range [1, MAX_WORK]
		disk_track = rand() % 16;
		disk_work = rand() % MAX_WORK + 1;

		clientReq->setTrackNum(disk_track);
		clientReq->setWorkSize(disk_work);

		//send the request to the monitor for processing
		req_passed = data->mon->addRequest(clientReq);

		cout << "Disk track is " << disk_track << ", Disk work is " << disk_work << endl;

		//wait for the worker to process the request
		//while(clientReq->getWorkFinished() == false){pthread_yield;}

		//result = request.getOutputData();
		
		cur_time = time(NULL);
	}
	pthread_exit(NULL);
}

int main(int argc, char **argv) {
    pthread_t thr;
    monitor* smon = new monitor();

    cout << "Creating thread..." << endl;

	threadInput client_data;
	client_data.tid = 1;
	client_data.mon = smon;
    pthread_create(&thr, NULL, client, (void*)&client_data);
    
    //pthread_create(&thr, NULL, client, NULL);
    
    pthread_join(thr, NULL);
};
